package com.wangtao.firstapp.fragments.original_author_fragments;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.adapters.AuthorCollectOneAdapter;
import com.wangtao.firstapp.adapters.AuthorCollectTwoAdapter;
import com.wangtao.firstapp.custom.FullyLinearLayoutManager;
import com.wangtao.firstapp.custom.ScRecyclerView;
import com.wangtao.firstapp.modles.OriginalAuthorCollect;
import com.wangtao.firstapp.utils.HttpUtils;

/**
 * 用户作者  介绍页面（收藏—>单品，文章）
 * Created by lanouhn on 16/9/7.
 */
public class ColectonFragment extends Fragment {
    private SharedPreferences sp;

    private ScRecyclerView myGridView;
    private AuthorCollectOneAdapter authorCollectOneAdapter;
    private OriginalAuthorCollect originalAuthorCollect;

    private TextView tv3;
    private ImageView colecton;


    private ScRecyclerView scRecyclerView;
    private AuthorCollectTwoAdapter authorCollectTwoAdapter;
    private OriginalAuthorCollect authorCollect;

    private LinearLayout lltColecton,lltEssay;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_colecton, container, false);
        myGridView = (ScRecyclerView) view.findViewById(R.id.gridView_fragment_collect);//收藏-->单品,的容器

        lltColecton= (LinearLayout) view.findViewById(R.id.lltColecton_fragment_colecton);//上部，显示or隐藏
        lltEssay= (LinearLayout) view.findViewById(R.id.lltEssay_fragment_coletion);//下部，显示or隐藏

        //lt_fragment_colecton = (LinearLayout) view.findViewById(R.id.lt_fragment_colecton);
        colecton = (ImageView) view.findViewById(R.id.colecton_fragment_collect);//全为空 时的，那个图
        tv3 = (TextView) view.findViewById(R.id.tv3_fragment_collect);//Ta还没有收藏过好物

        scRecyclerView = (ScRecyclerView) view.findViewById(R.id.scRecyclerView_fragment_collect);//收藏-->文章,的容器

        getDataFromWeb();
        initEvent();
        return view;

    }

    private void getDataFromWeb() {
        //打开文件
        sp = getActivity().getSharedPreferences("config", Context.MODE_PRIVATE);
        //接收作者ID
        String author_ID = sp.getString("author_iD", "2466368");

        //用户作者  介绍页面（收藏—>单品，文章）
        String urlCollect = "http://open4.bantangapp.com/users/center/collect";
        String paramsCollect = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&visit_user_id=" + author_ID + "&page=0&pagesize=20";

        try {
            //单品
            HttpUtils.doPostAsyn(urlCollect, paramsCollect, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson_collect = new Gson();
                    originalAuthorCollect = gson_collect.fromJson(result, OriginalAuthorCollect.class);

                    if (originalAuthorCollect.getData().getProduct().size() >0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(0);

                    }
                }
            });

            //文章
            HttpUtils.doPostAsyn(urlCollect, paramsCollect, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson_collect_essay = new Gson();
                    authorCollect = gson_collect_essay.fromJson(result, OriginalAuthorCollect.class);
                    if (authorCollect.getData().getTopic().size() > 0) {
                        handler.sendEmptyMessage(2);
                    } else {
                        handler.sendEmptyMessage(3);

                    }
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if (msg.what==0&&msg.what==3){
                tv3.setVisibility(View.VISIBLE);
                colecton.setVisibility(View.VISIBLE);
            }

            switch (msg.what) {
                case 0:
                    lltColecton.setVisibility(View.GONE);
                    break;
                case 3:
                    lltEssay.setVisibility(View.GONE);
                    break;
                case 1:
                    //lt_fragment_colecton.setVisibility(View.VISIBLE);
                    authorCollectOneAdapter = new AuthorCollectOneAdapter(getActivity(), originalAuthorCollect.getData().getProduct());
                    myGridView.setAdapter(authorCollectOneAdapter);
                    GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 3);
                    myGridView.setLayoutManager(gridLayoutManager);

                    break;
                case 2:
                    authorCollectTwoAdapter = new AuthorCollectTwoAdapter(getActivity(), authorCollect.getData().getTopic());
                    scRecyclerView.setAdapter(authorCollectTwoAdapter);
                    scRecyclerView.setLayoutManager(new FullyLinearLayoutManager(getActivity()));
                    break;
            }
        }
    };


    private void initEvent() {

    }
}
